package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.crop.Crop;
import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.URLHelper;
import com.zx.base.widget.CommentDialog;
import com.zx.base.widget.ExpanderDialog;
import com.zx.base.widget.ExpanderDialog.DialogListener;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class GoodsEditActivity extends BaseActivity {

	private TextView good_info_text_productTypeMin, good_info_text_productIntro, good_info_text_tradeWay, good_info_text_packIntro, good_info_edit_packIntro;

	private EditText good_info_edit_productName, good_info_edit_price, good_info_edit_telphone, good_info_edit_mobile, good_info_edit_num,
			good_info_edit_linkMan, good_info_edit_email, good_info_text_unit, good_info_edit_address;
	private LinearLayout good_info_line_productTypeMin, good_info_line_productIntro, good_info_line_tradeWay, good_info_line_packIntro, goods_edit_info_upload;
	private ImageView imView;

	private TradeApp app;
	private ExpanderDialog expander;
	private int NeworEdit = 0;
	private String goodid = "";
	private ProgressDialog progressDialog;
	private String productTypeMin="", tradeWay="";
	private CommentDialog comment;
	private Crop crop;

	private void init() {
		if (getIntent().getStringExtra("act").equals("new")) {

		} else if (getIntent().getStringExtra("act").equals("edit")) {
			NeworEdit = 1;
			goodid = getIntent().getStringExtra("goodid");
		}

		good_info_line_productTypeMin = (LinearLayout) findViewById(R.id.good_info_line_productTypeMin);
		good_info_line_productIntro = (LinearLayout) findViewById(R.id.good_info_line_productIntro);
		good_info_line_tradeWay = (LinearLayout) findViewById(R.id.good_info_line_tradeWay);
		good_info_line_packIntro = (LinearLayout) findViewById(R.id.good_info_line_packIntro);
		goods_edit_info_upload = (LinearLayout) findViewById(R.id.goods_edit_info_uploadImage);

		crop = new Crop(this);
		imView = (ImageView) findViewById(R.id.goods_edit_info_imageView);
		crop.setImageView(imView);
		goods_edit_info_upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				crop.showDialog();
			}
		});

		good_info_line_productTypeMin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expander = new ExpanderDialog(GoodsEditActivity.this, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						//
						good_info_text_productTypeMin.setText(group + " " + value);
						productTypeMin = app.typeids[groupid][valueid];
					}
				}, 2);
				expander.show();

			}
		});
		good_info_line_productIntro.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//
				comment = new CommentDialog(GoodsEditActivity.this, "产品简介", "提交", "取消", false);
				comment.show();
				comment.bt_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 提交评论
						String poll_con = comment.editor.getText().toString().trim();
						good_info_text_productIntro.setText(poll_con);
						comment.dlg.dismiss();
					}
				});
				comment.bt_no.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 取消
						comment.dlg.dismiss();
					}
				});

			}
		});
		good_info_line_tradeWay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//
				expander = new ExpanderDialog(GoodsEditActivity.this, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						//
						good_info_text_tradeWay.setText( app.tradeways[groupid]);
						tradeWay = app.tradewaysid[groupid];
					}
				}, 3);
				expander.show();
			}
		});

		good_info_line_packIntro.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//
				comment = new CommentDialog(GoodsEditActivity.this, "外观要求", "提交", "取消", false);
				comment.show();
				comment.bt_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 提交评论
						String poll_con = comment.editor.getText().toString().trim();
						good_info_text_packIntro.setText(poll_con);
						comment.dlg.dismiss();
					}
				});
				comment.bt_no.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 取消
						comment.dlg.dismiss();
					}
				});

			}
		});
		good_info_text_packIntro = (TextView) findViewById(R.id.good_info_text_packIntro);
		good_info_text_productTypeMin = (TextView) findViewById(R.id.good_info_text_productTypeMin);
		good_info_text_productIntro = (TextView) findViewById(R.id.good_info_text_productIntro);
		good_info_text_tradeWay = (TextView) findViewById(R.id.good_info_text_tradeWay);
		good_info_edit_packIntro = (TextView) findViewById(R.id.good_info_text_packIntro);

		good_info_text_unit = (EditText) findViewById(R.id.good_info_text_unit);
		good_info_edit_productName = (EditText) findViewById(R.id.good_info_edit_productName);
		good_info_edit_price = (EditText) findViewById(R.id.good_info_edit_price);
		good_info_edit_telphone = (EditText) findViewById(R.id.good_info_edit_telphone);
		good_info_edit_mobile = (EditText) findViewById(R.id.good_info_edit_mobile);
		good_info_edit_num = (EditText) findViewById(R.id.good_info_edit_num);
		good_info_edit_linkMan = (EditText) findViewById(R.id.good_info_edit_linkman);
		good_info_edit_email = (EditText) findViewById(R.id.good_info_edit_email);
		good_info_edit_address = (EditText) findViewById(R.id.good_info_edit_address);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.goods_edit_info);
		init();
		setHeader(1);
		app = (TradeApp) getMyApplication();
		this.header_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				progressDialog = ProgressDialog.show(GoodsEditActivity.this, null, "正在提交数据，请稍后.....", true, true);
				editGoodInfoTask();
			}
		});
		if (NeworEdit == 1) {
			progressDialog = ProgressDialog.show(GoodsEditActivity.this, null, "正在接收数据，请稍后.....", true, true);
			readGoodInfoTask(goodid);
		}
	}

	private void editGoodInfoTask() {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("act", "productEdit");
		map.put("userId", app.user.getId());
		if (NeworEdit == 1) {
			map.put("id", goodid);
		}
		map.put("productName", good_info_edit_productName.getText().toString().trim());
		map.put("price", good_info_edit_price.getText().toString().trim());
		map.put("num", good_info_edit_num.getText().toString().trim());
		map.put("unit", good_info_text_unit.getText().toString().trim());
		map.put("productIntro", good_info_text_productIntro.getText().toString().trim());
		map.put("packIntro", good_info_edit_packIntro.getText().toString().trim());
		map.put("productTypeMin",productTypeMin);
		map.put("linkMan", good_info_edit_linkMan.getText().toString().trim());
		map.put("tradeWay", tradeWay);
		map.put("telphone", good_info_edit_telphone.getText().toString().trim());
		map.put("mobile", good_info_edit_mobile.getText().toString().trim());
		map.put("email", good_info_edit_email.getText().toString().trim());
		map.put("address", good_info_edit_address.getText().toString().trim());
		map.put("photo", ((BitmapDrawable) imView.getDrawable()).getBitmap());
		new UploadImage().execute(map);
	}

	private void readGoodInfoTask(String goodsId) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "productDetail");
		map.put("id", goodsId);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseGoodInfoJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(GoodsEditActivity.this, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}

	private void parseGoodInfoJson(JSONObject response) {
		try {
			JSONObject json = response;
			String succeed = json.getString("succeed");
			if (succeed.equals("000")) {
				good_info_text_productIntro.setText(json.getString("intro"));
				good_info_text_unit.setText(json.getString("unit"));
				good_info_text_tradeWay.setText(json.getString("tradeWay"));
				good_info_edit_packIntro.setText(json.getString("packIntro"));
				good_info_edit_productName.setText(json.getString("name"));
				good_info_edit_price.setText(json.getString("price"));
				good_info_edit_telphone.setText(json.getString("telphone"));
				good_info_edit_mobile.setText(json.getString("mobile"));
				good_info_edit_num.setText(json.getString("num"));
				good_info_edit_linkMan.setText(json.getString("linkMan"));
				good_info_edit_email.setText(json.getString("email"));
				good_info_edit_address.setText(json.getString("address"));
				good_info_text_productTypeMin.setText(json.getString("productTypeMax")+json.getString("productTypeMin"));
				app.imageLoader.DisplayImage(URLHelper.mPrefixImg + response.getString("photo"), imView, R.drawable.icon_upload);
				
				productTypeMin=json.getString("productTypeMinId");
				if(productTypeMin.equals("")){
					productTypeMin=json.getString("productTypeMaxId");
				}
				tradeWay=json.getString("tradeWayId");

			} else if (succeed.equals("002")) {
				Toast toast = Toast.makeText(GoodsEditActivity.this, "数据读取失败。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				Toast toast = Toast.makeText(GoodsEditActivity.this, "后台出错,请联系系统管理员。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		} catch (JSONException e) {
			if(Constant.debug)Log.d("bug", "bug");
			e.printStackTrace();
		} finally {
			if (progressDialog != null)
				progressDialog.dismiss();
			progressDialog = null;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		crop.onActivityResult(requestCode, resultCode, data);
	}

	class UploadImage extends AsyncTask<Map<String, Object>, Void, String> {

		@Override
		protected String doInBackground(Map<String, Object>... params) {
			return HttpUtils.postImage(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				JSONObject object = new JSONObject(result);
				String succeed = object.getString("succeed");
				if (succeed.equals("000")) {
					Toast toast = Toast.makeText(GoodsEditActivity.this, "提交成功.", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					goback();
				}else{
					Toast toast = Toast.makeText(GoodsEditActivity.this, "数据格式错误,请检查.", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} finally {
				if (progressDialog != null)
					progressDialog.dismiss();
				progressDialog = null;
			}
		}

		@Override
		protected void onCancelled() {
			if(Constant.debug)Log.d("debug", "onCancelled");
		}

	}
}
